BLACK = 0
WHITE = 0xffff
BLUE = 0x001f

SCREEN_WIDTH = 128
SCREEN_HEIGHT = 128

-- 屏幕的有效刷新区起始坐标总是为(256,256)
VSCREEN_X = SCREEN_WIDTH * 2
VSCREEN_Y = SCREEN_HEIGHT * 2

-- 创建3个图层，2个RGB565 1个RGBA(3倍屏宽用来实现主菜单滑动)
disp.createlayer(disp.LAYER_ID_0, disp.LAYER_FORMAT_RGB565, 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1)
disp.createlayer(disp.LAYER_ID_1, disp.LAYER_FORMAT_RGB565, 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1)
disp.createlayer(disp.LAYER_ID_2, disp.LAYER_FORMAT_RGBA, 0, 0, 3 * SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1)
disp.setlayershow(true, false, false)
disp.setlayerstartpos(disp.LAYER_ID_0, VSCREEN_X, VSCREEN_Y)
disp.setactivelayer(disp.LAYER_ID_0)

local function getxpos(s)
    return (SCREEN_WIDTH - s:len() * 8) / 2
end

raw_disp_putimage = disp.putimage
disp.putimage = function(name, x, y, transcolor, pathexist)
    raw_disp_putimage(pathexist and name or '/ldata/' .. name, x, y, transcolor)
end

raw_disp_puttext = disp.puttext
disp.puttext = function(text, x, y)
    return raw_disp_puttext(text, x or getxpos(text), y or 56)
end

local disp_locked = false
disp.lock = function() disp_locked = true end
disp.unlock = function() disp_locked = false end

raw_disp_update = disp.update
disp.update = function(x1, y1, x2, y2)
    if disp_locked then return end
    x1 = x1 or 0
    y1 = y1 or 0
    x2 = x2 or 127
    y2 = y2 or 127
    raw_disp_update(VSCREEN_X + x1, VSCREEN_Y + y1, VSCREEN_X + x2, VSCREEN_Y + y2, x1, y1)
end

raw_disp_init = disp.init
disp.init = function(...)
    raw_disp_init(unpack(arg))
    -- 加载大字体 用于待机时间与秒表显示 字体加载必须在disp.init之后执行
    disp.font_16x32 = disp.loadfont("/ldata/16x32_0-9.bin")
end
